/*
   Copyright (C) 2009 Hugh Nelson.
   This file is part of Libbarteta.

   Libbarteta is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   Libbarteta is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Libbarteta.  If not, see <http://www.gnu.org/licenses/>.
*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "internal.h"

barteta_handle barteta_getHandle( void )
{
   barteta_handle bh = calloc( 1, sizeof( barteta_handle ) );
   return bh;
}

void barteta_releaseHandle( barteta_handle bh )
{
   xmlFreeDoc( bh->doc );
   free( bh );
}

int barteta_getData( barteta_handle bh )
{
   LIBXML_TEST_VERSION

      bh->doc = xmlReadFile( "http://bart.gov/dev/eta/bart_eta.xml", NULL, 0 );

   if( bh->doc )
      return 0;
   else
      return 1;
}

void handleEta( barteta_station* bs, xmlNodePtr eta )
{
   xmlNodePtr curr = NULL;

   barteta_dest* dest = calloc( 1, sizeof( barteta_dest ) );

   for( curr = eta->children; curr; curr = curr->next )
   {
      if( eta->type != XML_ELEMENT_NODE )
	 continue;

      xmlChar* content = xmlNodeGetContent( curr );

      if( !content )
      {
	 printf( "no content\n" );
	 continue;
      }

      if( !strcmp( curr->name, "destination" ) ) dest->name = content;
      if( !strcmp( curr->name, "estimate" ) ) dest->estimates = content;
   }

   bs->dests[ bs->numDests++ ] = dest;
}

barteta_station* barteta_getStation( barteta_handle bh, char* stationAbbr )
{

   const char* xpFmt = "//abbr[text()='%s']/parent::station";
   char xpStr[100];
   memset( xpStr, 0, sizeof( xpStr ) );

   snprintf( xpStr, sizeof( xpStr ), xpFmt, stationAbbr );

   barteta_station* bs = calloc( 1, sizeof( barteta_station ) );

   xmlXPathContextPtr ctx = xmlXPathNewContext( bh->doc );
   xmlXPathObjectPtr xpObj = xmlXPathEvalExpression( xpStr, ctx );
   xmlNodePtr station = xpObj->nodesetval->nodeTab[ 0 ];

   xmlNodePtr curr = NULL;

   for( curr = station->children; curr; curr = curr->next )
   {
      if( curr->type != XML_ELEMENT_NODE )
	 continue;

      xmlChar* content = xmlNodeGetContent( curr );

      if( !content )
      {
	 printf( "no content\n" );
	 continue;
      }

      if( !strcmp( curr->name, "name" ) ) bs->name = content;
      if( !strcmp( curr->name, "abbr" ) ) bs->abbr = content;
      if( !strcmp( curr->name, "date" ) ) bs->date = content;
      if( !strcmp( curr->name, "time" ) ) bs->time = content;
      if( !strcmp( curr->name, "eta" ) ) handleEta( bs, curr );
   }

   xmlXPathFreeObject( xpObj );
   xmlXPathFreeContext( ctx );

   return bs;
}

void barteta_releaseStation( barteta_station* bs )
{
   xmlFree( bs->name );
   xmlFree( bs->abbr );
   xmlFree( bs->date );
   xmlFree( bs->time );

   int curr;

   for( curr = 0; curr < bs->numDests; curr++ )
   {
      xmlFree( bs->dests[ curr ]->name );
      xmlFree( bs->dests[ curr ]->estimates );
      free( bs->dests[ curr ] );
   }

   free( bs );
}

